﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CoreData;
using System.Reflection;
using System.IO;

namespace SchoolManagementSystem.Forms
{
    public partial class frmAudit : DevExpress.XtraEditors.XtraForm
    {
        string layout = Application.StartupPath + "\\Layout\\";
        string code;
        bool _layoutRestored;
        public frmAudit(string code)
        {
            InitializeComponent();
            this.code = code;
            LoadData();
        }
        void LoadData()
        {
            var model = RegisterService.RegisterServices.AuditService;
            var data = model.GetByCode(code) as List<Audit>;

            if (data == null)
            {
                data = new List<Audit>();
            }


            gridControl1.DataSource = data;
            gridControl1.RefreshDataSource();
            RestoreLayout();
            gridView1.ViewCaption = "Danh sách có " + gridView1.RowCount + " dòng";
            if (gridView1.RowCount <= 0)
            {
                gridView1.ViewCaption = "Không có dữ liệu";
            }
            PropertyInfo[] propertyInfos;
            propertyInfos = new Audit().GetType().GetProperties(); ;

            foreach (PropertyInfo proInfo in propertyInfos)
            {
                AttributeCollection attributes = TypeDescriptor.GetProperties(new Audit().GetType())[proInfo.Name].Attributes;

                DescriptionAttribute myAttribute =
                   (DescriptionAttribute)attributes[typeof(DescriptionAttribute)];
                if (String.IsNullOrEmpty(myAttribute.Description))
                {
                    gridView1.Columns[proInfo.Name].Visible = false;
                }
                else
                {
                    gridView1.Columns[proInfo.Name].Caption = myAttribute.Description;
                    gridView1.Columns[proInfo.Name].OptionsColumn.AllowEdit = false;
                }
            }
          
          //  gridView1.ExpandAllGroups();

        }


        void RestoreLayout()
        {
            if (!_layoutRestored)
            {
                layout = Application.StartupPath + "\\Layout\\frmaudit.GID";

                if (gridView1.RowCount > 0 && File.Exists(layout))
                {
                    gridView1.RestoreLayoutFromXml(layout);
                    _layoutRestored = true;
                }
               
            }
        }

        private void SaveLayouts()
        {
            try
            {
                gridView1.SaveLayoutToXml(layout);
            }
            catch (Exception)
            {

            }
        }

        private void frmAudit_FormClosing(object sender, FormClosingEventArgs e)
        {
            SaveLayouts();
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                if (gridView1.RowCount != 0)
                {
                    long ID = long.Parse(gridView1.GetRowCellValue(e.FocusedRowHandle, "ID").ToString());
                    var data = RegisterService.RegisterServices.AuditService.GetByID(ID) as Audit;
                    txtacc.Text = data.UserExt;
                    txtaction.Text = data.Action;
                    txtdate.Text = data.ModifiedDate.ToLongDateString();
                    txtmodule.Text = data.Module;
                    txtcontent.Text = data.Description;


                }
            }
            catch { }
        }



    }
}
